package uiLayer;
import modelLayer.*;
import controlLayer.*;
import java.util.Scanner;
/**
 * UI class that handles the IO of the DVD menu. Interacts with the DVDController in the controlLayer. 
 * 
 * @author (Gruppe E) 
 * @version (06-11-2014)
 */
public class DvdUI
{
    private DVDController dvdCtr;

    /**
     * Constructor for DvdUI.
     */
    public DvdUI(DVDController controller)
    {
        dvdCtr = controller;
    }

    /**
     * Starts the dvdMenu UI.
     */
    public void start()
    {
        dvdMenu();
    }

    /**
     * Runs a while-loop, supporting IO interactions with the user.
     */
    private void dvdMenu()
    {
        boolean exit = false;

        while(!exit)
        {
            String choice = writeDvdMenu();
            if(choice.equals("1"))
            {
                addDVD();
            }

            else if(choice.equals("2"))
            {
                findDVD();
            }

            else if(choice.equals("3"))
            {
                updateDVD();
            }

            else if(choice.equals("4"))
            {
                deleteDVD();
            }

            else if(choice.equals("5"))
            {
                exit = true;
            }

            else
            {
                System.out.println("\nInvalid types. Please try again.");
            }
        }
    }

    /**
     * Displays a set of options and prompts an integer value. 
     */
    private String writeDvdMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        blankLines();
        System.out.println("*** DvdMenu ***");
        System.out.println("1. Add DVD");
        System.out.println("2. Find DVD");
        System.out.println("3. Update DVD");
        System.out.println("4. Delete DVD");
        System.out.println("5. Back");
        System.out.println("\nPlease choose a number");

        String choice = keyboard.nextLine();
        return choice;
    }

    /**
     * Requests the DVDController to create a new DVD object with the input values as parameters.
     */
    private void addDVD()
    {
        blankLines();
        System.out.println("\nEnter the information about the DVD.");
        String barcode = inputBarcode();
        if(dvdCtr.findDVD(barcode) == null)
        {
            String title = inputTitle();
            String artist = inputArtist();
            String publicationDate = inputPublicationDate();
            dvdCtr.createDVD(barcode, title, artist, publicationDate);
            System.out.println("\nDVD succesfully created.");
        }
        else
        {
            System.out.println("\nA DVD with that barcode already exists.");
        }
    }

    /**
     * Requests the DVDController to find a DVD object. Writes the information about a DVD to the UI
     * if it exists, else writes a "failed" message.
     */
    private void findDVD()
    {
        blankLines();
        System.out.println("\nEnter the barcode on the DVD you wish to find.");
        String barcode = inputBarcode();
        DVD dvd = dvdCtr.findDVD(barcode);
        if(dvd != null)
        {
            System.out.println("Dvd found \n");
            System.out.println("Title: " + dvd.getTitle());
            System.out.println("Artist: " + dvd.getArtist());
            System.out.println("Publication Date: " + dvd.getPublicationDate());
            System.out.println("Barcode: " + dvd.getBarcode());
        }

        else
        {
            System.out.println("\nDVD not found. Please try again.");
        }

    }

    /**
     * Requests the DVDController to update a DVD object using input from the user.
     */
    private void updateDVD()
    {
        blankLines();
        System.out.println("Please choose the DVD you wish to update");
        String barcode = inputBarcode();
        if(dvdCtr.findDVD(barcode) != null)
        {
            System.out.println("\nEnter the new information to change DVD information: \n");
            String title = inputTitle();
            String newBarcode = inputBarcode();
            String artist = inputArtist();
            String publicationDate = inputPublicationDate();
            dvdCtr.updateDVD(barcode, title, artist, publicationDate, newBarcode);
            System.out.println("\nDVD succesfully updated.");
        }
        else
        {
            System.out.println("\nDVD not found.");
        }
    }

    /**
     * Requests the DVDController to delete a DVD object.
     */
    private void deleteDVD()
    {
        blankLines();
        System.out.println("\nEnter the barcode of the DVD you wish to delete");
        String barcode = inputBarcode();
        if(dvdCtr.deleteDVD(barcode))
        {
            System.out.println("\nDVD succesfully deleted");
        }
        else
        {
            System.out.println("\nCould not find DVD to delete");
        }
    }

    /**
     * Prompts the user to enter a title string.
     * @return  String
     */
    private String inputTitle()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the title of the DVD: ");
        String title = scanner.nextLine();
        return title;
    }

    /**
     * Prompts the user to enter a barcode string.
     * @return String
     */
    private String inputBarcode()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the barcode of the DVD: ");
        String barcode = scanner.nextLine();
        return barcode;
    }

    /**
     * Prompts the user to enter a publication-date string.
     * @return  String
     */
    private String inputPublicationDate()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the publication date: ");
        String date = scanner.nextLine();
        return date;
    }

    /**
     * Prompts the user to enter an artist string.
     * @return  String 
     */
    private String inputArtist()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the artist of the DVD: ");
        String artist = scanner.nextLine();
        return artist;
    }
    
    /**
     * This method adds 5 blank lines.
     */
    private void blankLines()
    {
        for(int i = 0; i < 5; i++)
        {
            System.out.println();
        }
    }
}
